﻿use strict;

use LWP::Simple;
use HTML::TokeParser;
use XML::RSS;
use DateTime;
use Encode;

my $content = get("http://www.dijanet.net/seeding.html") or die $!;
my $stream = HTML::TokeParser->new(\$content ) or die $!;
my $rss = XML::RSS->new (version => '1.0' );

$rss->channel(
    title => "nn64 漫畫列表", link => "http://www.dijanet.net", description => "漫畫列表監視器 comic torrent news",
    dc => { date => DateTime->now, subject => 'japanese comics torrent news', creator => 'chunju+nn64@gmail.com', publisher => 'jackykkk@hotmail.com', language => 'zh-TW'});

my (@itemlist, %item);
my ($token, $tag, $headline, $url, $i);
my ($y, $m, $d);
$y = DateTime->now->year;
$m = DateTime->now->mon;
$d = DateTime->now->day;

$tag = $stream->get_tag('b');
$tag = $stream->get_tag('br');
$tag = $stream->get_tag('br');

my $goon;
$goon = 1;
while($goon)
{
    $token = $stream->get_token();
    if( ($token->[0] eq "T" || $token->[0] eq "S") )
    {
         $stream->unget_token(($token));
         $headline = $stream->get_trimmed_text('a');
         $tag = $stream->get_tag('a');               
         $url = "http://www.dijanet.net/stealer1/".$tag->[1]{href};
         $headline .= $stream->get_trimmed_text('br');
         Encode::from_to($headline, "big5", "utf8");
         $tag = $stream->get_tag('br');
         $itemlist[$#itemlist+1]={headline=>$headline, url=>$url};
         $i++;

         $token = $stream->get_token();
    }
    elsif( ($token->[0] eq "C") ) 
    {
        $token->[1]=~/<!--(.*)-->/;
        my @parts = split(/\//, $1);
        my $old;
        if( ($y-@parts[0])>0 ) {$old = "too old";}
        elsif( ($m-@parts[1])>0 ) {$old = $m-@parts[1]." months old";}
        elsif( ($d-@parts[2])>0 ) {$old = $d-@parts[2]." days old";}
        else { $old = 'new!';}
        my $date = DateTime->new(year => @parts[0], month => @parts[1], day => @parts[2]);
        foreach (@itemlist)
        {
             if( !$_->{old}) {$_->{old} = $old; $_->{date} = $date;}
        }

        if($i > 10) {$goon = "";}
    }
}

foreach (@itemlist)
{
# print $_->{headline}, "--", $_->{old}, "\n";
    $rss->add_item( title => $_->{headline}.'('.$_->{old}.')', link => $_->{url}, dc => {date => $_->{date} });
}

$rss->save("nn64.rss") or die$!;
my $now = DateTime->now;
print "nn64.rss created -$now\n";
